<?php

namespace App\Jobs;

use App\Model\Device;
use App\Model\DeviceWarning;
use App\Model\House;
use App\Utils\Result;
use Faker\Core\DateTime;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;


class CancelWarning implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $deviceId;
    public $tries = 2;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($deviceId)
    {
        $this->deviceId = $deviceId;
        //因为要导入大文件 需要很久时间
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try{
            $today = $this->deviceId.'-'.date('Ymd');
            $device = Device::query()->where('id',$this->deviceId)->first();
            $device->cancel_data = date('Y-m-d');
            $device->save();
            if(Redis::exists($today)){
                $xh = Redis::get($today);
                $cancel_power = Redis::get('LOW_CANCEL_WARNING');
                Log::info('device_id:'.$this->deviceId.'----xh:'.$xh.'cancel_power:'.$cancel_power);
                if($xh > $cancel_power){
                    $device->is_warning = 0;
                    $device->save();
                    $warning = DeviceWarning::query()->where('device_id',$this->deviceId)
                        ->where('re_xh',0)
                        ->orderBy('id','desc')
                        ->first();
                    if($warning){
                        $warning->re_time = date('Y-m-d');
                        $warning->re_xh = $xh;
                        $warning->save();
                    }
                }
            }
        }catch (\Exception $e){
            Log::info($e->getMessage());
        }
    }
}

